<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Admin extends CI_Controller {
	
	public function __construct()
    {
        parent::__construct();
		$this->load->helper('cookie');
		$this->load->library('session');
		$this->load->model('stu_model');
		$this->load->model('th_model');
    }

	public function index()
	{ 
		$res = $this->allcklg();
		if($res['username']&&$res['userid']){
			$data['name'] = $res;
			$this->load->view('admin/index',$data);
		}else{
			redirect('admin/login');
		}	
	}

	//添加学生表单
	public function addform(){
		$data['id'] = $this->session->userid;
		$this->load->view('admin/addform',$data);
	}

	//添加学生到数据库
	public function addstu(){
		$data = $this->input->post();
		$bool = $this->stu_model->insert($data);
		
		if($bool){
			echo "添加成功！";
			$this->load->view('admin/addform');
		}else{
			echo "添加失败";
			$this->load->view('admin/addform');
		}
	}

	//学生列表
	public function stulist(){
		$res = $this->stu_model->getall();
		$data['stus']=$res->result();
		
		foreach($data['stus'] as $item){
			$id= $item->userid;
			//echo $this->getnameth($id);
			$item->userid=$this->getnameth($id);
		}
				
		$data['num']=$res->num_rows();
		$this->load->view('admin/stulist',$data);
	}

	//删除学生
	public function delete($id){
		$bool = $this->stu_model->delete($id);
		if($bool){
			echo "删除成功<br>";
			$this->stulist();
		}else{
			echo "删除失败<br>";
			$this->stulist();
		}
	}

	//编辑学生
	public function edit($id){
		$data['stu'] = $this->stu_model->getone($id);
		$data['id'] = $this->session->userid;
		$this->load->view('admin/edit',$data);
	}

	//更新学生
	public function editstu(){
		$data = $this->input->post();
		$bool = $this->stu_model->updata($data);
		if($bool){
			echo "更新成功";
			$this->stulist();
		}else{
			echo "操作失败";
			$this->stulist();
		}
	}

	//学生查找页面
	public function search(){
		$this->load->view('admin/search');
	}
	
	//返回查找结果
	public function seastu(){
		$data = $this->input->post();
		$res = $this->stu_model->search($data);
		$data['stus']=$res->result();
		$data['num']=$res->num_rows();
		
		foreach($data['stus'] as $item){
			$id= $item->userid;
			$item->userid=$this->getnameth($id);
		}

		if($data['num']){
			$this->load->view('admin/stulist',$data);
		}else{
			echo "没有找到相关信息";
			$this->search();
		}
	
	}

	//添加教师页面
	public function addth(){
		$this->load->view('admin/addth');
	}
	

	//添加教师
	public function addths(){
		$data = $this->input->post();
		$bool = $this->th_model->insert($data);
		
		if($bool){
			echo "添加成功";
		}else{
			echo "操作失败";
			$this->addth();
		}
	}

	//教师列表
	public function thlist(){
		$res = $this->th_model->getall();
		$data['ths']=$res->result();
		$data['num']=$res->num_rows();
		$this->load->view('admin/thlist',$data);
	}

	//删除教师
	public function delth($id){
		$bool = $this->th_model->delete($id);
		if($bool){
			echo "删除成功<br>";
			$this->thlist();
		}else{
			echo "删除失败<br>";
			$this->thlist();
		}
	}

	//编辑教师
	public function edith($id){
		$data['ths'] = $this->th_model->getone($id);
		$this->load->view('admin/edith',$data);
	}

	//更新教师
	public function editths(){
		$data = $this->input->post();
		$bool = $this->th_model->updata($data);
		if($bool){
			echo "更新成功";
			$this->thlist();
		}else{
			echo "操作失败";
			$this->thlist();
		}
	}

	//id获取教师姓名
	public function getnameth($id){
		$res = $this->th_model->getname($id);
		if($res){
			return $res->name;	
		}else{
			return "";
		}
	}

	//登录页
	public function login(){
		$res = $this->allcklg();
		if($res['username']&&$res['userid']){
			redirect('admin/index');
		}else{
			$this->load->helper('captcha');
			$vals = array(
			'img_path' => './captcha/',
			'img_url' => base_url().'captcha/',
			'word_length'   => 2,
			'expiration'    => 60,
			'img_width' => '80'
			);
			$data['cap'] = create_captcha($vals);
			$this->session->capword = $data['cap']['word'];
			$this->load->view('admin/login',$data);
		}


		
	}

	//登录验证
	public function cklogin(){
		$cpa = $this->input->post('cpa');
		$capword = $this->session->capword;
		if($cpa != $capword){
				echo "验证码错误！";
				exit;
		}
		$name = $this->input->post('name');
		$password = md5($this->input->post('password'));
		$res = $this->th_model->ckth($name);
		if($res){
			if($password!=$res->password){
					echo "密码错误！";
					echo $password."--------".$res->password;
					exit;
			}else{
				$this->session->name=$name;
				$this->session->userid=$res->id;
				$cookie = array(
					'name'   => 'username',
					'value'  => $name,
					'expire' => '80000'
				);
				$this->input->set_cookie($cookie);
				$cookie2 = array(
					'name'   => 'userid',
					'value'  => $res->id,
					'expire' => '80000'
				);
				$this->input->set_cookie($cookie2);
				redirect('admin/index');
			}
		}else{
			echo "账号不存在";
			exit;
		}

	}

	//登录通用验证
	public function allcklg(){
		$res = $this->input->cookie(array('username','userid'));
		return $res;
		
	}
	
	//退出
	public function loginout(){
		$this->input->set_cookie('username','');
		$this->input->set_cookie('userid','');
		$this->session->unset_userdata('name');
		$this->session->unset_userdata('userid');
		//redirect('admin/login');
		$this->login();
	}
	

}